/*
* $Id: QueenBeeTable.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QueenBeeTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CQueenBeeTable::CQueenBeeTable()
: CTable(QUEEN_BEE_TABLE_NAME){}
CQueenBeeTable::~CQueenBeeTable(){}

void CQueenBeeTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
						_T("(id_queen_bee INTEGER NOT NULL,")\
						_T("description TEXT NULL,")\
						_T("id_parent_queen_bee INTEGER NULL,")\
						_T("date DATE NOT NULL,")\
						_T("breed INTEGER NULL,")\
						_T("PRIMARY KEY(id_queen_bee ASC AUTOINCREMENT),")\
						_T("CONSTRAINT ftQueenBee")\
						_T("	FOREIGN KEY(id_parent_queen_bee)")\
						_T("	REFERENCES queen_bee(id_queen_bee));"));
}
void CQueenBeeTable::UpdateValue(IDatabase* pDatabase){
	//std::tstring strRequest(_T("UPDATE "));
	//CSqlBinder binder(strRequest + GetNameTable() + 
	//	_T(" SET honey=?, plant=? WHERE id_family=? AND date=?;"));
	//binder.SetValue(1, m_content.s_strPlant);
	//binder.SetValue(3, m_content.s_date, true);
	//binder.SetValue(0, m_content.s_nHoney);
	//binder.SetValue(2, m_content.s_nIdFamily);
	//pDatabase->ExecRequest(binder.GetRequest());
	//m_content.Clear();
}
IResult* CQueenBeeTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_nId){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_queen_bee=?"));
		binder.SetValue(0, m_content.s_nId);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CQueenBeeTable::SetValue(IDatabase* pDatabase){
	//CSqlBinder binder;
	//std::tstring strRequest(_T("INSERT INTO "));
	//binder.SetRequest(strRequest + GetNameTable() + _T(" VALUES(?, ?, ?, ?)"));
	//binder.SetValue(0, m_content.s_date, true);
	//binder.SetValue(1, m_content.s_nIdFamily);
	//binder.SetValue(2, m_content.s_nHoney);
	//if(m_content.s_strPlant.empty())
	//	binder.SetValue(3);
	//else
	//	binder.SetValue(3, m_content.s_strPlant);
	//m_content.Clear();
	//pDatabase->ExecRequest(binder.GetRequest());
}
void CQueenBeeTable::DeleteValue(IDatabase* pDatabase){
	//CSqlBinder binder;
	//std::tstring strRequest(_T("DELETE FROM "));
	//if(m_content.s_date){
	//	strRequest = strRequest + GetNameTable() + _T(" WHERE date=?");
	//	binder.SetValue(0, m_content.s_date, true);
	//	if(m_content.s_nIdFamily){
	//		strRequest = strRequest + _T(" AND id_family=?");
	//		binder.SetValue(1, m_content.s_nIdFamily);
	//	}
	//	binder.SetRequest(strRequest);
	//}else{
	//	binder.SetRequest(strRequest + GetNameTable());
	//}
	//m_content.Clear();
	//pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CQueenBeeTable::CreateContent(){
	return new content;
}

void CQueenBeeTable::content::Clear(){
	s_nId = 0;
	s_strDiscription.clear();
	s_date = 0;
	s_nIdParentQueenBee = 0;
	s_breed = HYBRID;
}
void CQueenBeeTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("date"))
			s_date = CConvertFunction::ConvertFromDate(it->m_strValueColumn);
		else if(it->m_strNameColumn == _T("id_queen_bee"))
			s_nId = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("description"))
			s_strDiscription = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("breed"))
			s_breed = (breed)_ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("id_parent_queen_bee"))
			s_nIdParentQueenBee = _ttoi(it->m_strValueColumn.c_str());
	}
}
void CQueenBeeTable::content::Free(){
	delete this;
}